package com.viewpagerindicator.sample.adapters;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.viewpagerindicator.sample.PrintActivity;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.beans.Devices;
import com.viewpagerindicator.sample.beans.Goods;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;


/**
 * Created by zhao on 2015/11/7.
 */
public class DeviceListAdapter extends ArrayAdapter<BluetoothDevice> {
    private int resource;
    private LayoutInflater inflater;
    public DeviceListAdapter(Context context, int resource, List<BluetoothDevice> objects) {
        super(context, resource, objects);
        this.resource=resource;
        this.inflater=LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflater.inflate(resource, null);
        }
        BluetoothDevice bluetoothDevice=getItem(position);

        TextView textView= (TextView) convertView.findViewById(R.id.goods_item_name);
        textView.setText(getItem(position).getName());

        TextView desctextView= (TextView) convertView.findViewById(R.id.type_item_desc);
        desctextView.setText(getItem(position).getAddress());

        Button button= (Button) convertView.findViewById(R.id.device_pair);
        int connectState = bluetoothDevice.getBondState();
        switch (connectState){
            // 未配对
            case BluetoothDevice.BOND_NONE:
                button.setText(R.string.btn_bluetooth_peidui);
//                setOncClick(button,bluetoothDevice);
                break;
            // 已配对
            case BluetoothDevice.BOND_BONDED:
                button.setText(R.string.btn_bluetooth_peiduied);
                break;
        }
        return convertView;
    }
//    public void setOncClick(Button button, final BluetoothDevice device){
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                device.createBond();
//                Intent intent=new Intent(getContext(), PrintActivity.class);
//                intent.putExtra(PrintActivity.EXTRAS_DEVICE_ADDRESS,device.getAddress());
//                ((Activity)getContext()).startActivity(intent);
//            }
//        });
//    }
}
